﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Caliburn.Micro;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using Caliburn.Micro.BikeWizard.Utils;

namespace Caliburn.Micro.BikeWizard.Data
{
    public class BikeOrder : PropertyChangedBase
    {
        #region Constructors
        public BikeOrder()
        {
            this.Extras = new ObservableCollection<ExtrasOption>();
        }
        #endregion

        #region Properties
        private BikeTypeOption bikeType;
        public BikeTypeOption BikeType
        {
            get { return bikeType; }
            set
            {
                bikeType = value;
                price = GetTotalPrice();
                NotifyOfPropertyChange(() => BikeType);
            }
        }

        private FrameSizeOption frameSizeOption;
        public FrameSizeOption FrameSize
        {
            get { return frameSizeOption; }
            set
            {
                frameSizeOption = value;
                price = GetTotalPrice();
                NotifyOfPropertyChange(() => FrameSize);
            }
        }

        private PartsGroupOption partsGroup;
        public PartsGroupOption PartsGroup
        {
            get { return partsGroup; }
            set
            {
                partsGroup = value;
                price = GetTotalPrice();
                NotifyOfPropertyChange(() => PartsGroup);
            }
        }

        private ObservableCollection<ExtrasOption> extras;
        public ObservableCollection<ExtrasOption> Extras
        {
            get { return extras; }
            private set
            {
                extras = value;
                extras.CollectionChanged += Extras_CollectionChanged;
            }
        }

        private decimal price = 0;
        public decimal Price
        {
            get 
            { 
                return price; 
            }
            private set
            {
                price = value;
                NotifyOfPropertyChange(() => Price);
            }
        }
        #endregion

        #region Private Methods
        private decimal GetTotalPrice()
        {
            decimal totalPrice = 0;

            if (this.BikeType != null)
            {
                totalPrice += this.BikeType.Price;
            }

            if (this.FrameSize != null)
            {
                totalPrice += this.FrameSize.Price;
            }

            if (this.PartsGroup != null)
            {
                totalPrice += this.PartsGroup.Price;
            }

            if (this.Extras != null)
            {
                foreach (var extra in this.Extras)
                {
                    totalPrice += extra.Price;
                }
            }

            return totalPrice;
        }

        private void Extras_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            NotifyOfPropertyChange(() => Extras);
            price = GetTotalPrice();
        }
        #endregion

        #region Object Overrides
        public override string ToString()
        {
            return 
                String.Format(
                    "A {0} sized {1} with {2} and the extras: {3}", 
                    this.BikeType,
                    this.FrameSize,
                    this.PartsGroup,
                    CollectionHelper.ToString(this.Extras));
        }
        #endregion
    }
}
